package com.example.smartmallserver.demos.web.service.impl;

import com.example.smartmallserver.demos.web.mapper.IndexMapper;
import com.example.smartmallserver.demos.web.pojo.*;
import com.example.smartmallserver.demos.web.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService  {
    @Autowired
    IndexMapper indexMapper;
    @Override
    public List<CarouselImage> getCarouselImages() {
        return indexMapper.getCarouselImages();

    }

    @Override
    public List<Navigation> getNavigations() {
        return indexMapper.getNavigations();
    }

    @Override
    public List<Goods> getGoods() {
        return indexMapper.getGoods();
    }

    @Override
    public List<Goods> getGoodsByName(String name) {
        return indexMapper.getGoodsByName(name);
    }

    @Override
    public List<Goods> getGoodsByCategoryId(Integer categoryId) {
        return indexMapper.getGoodsByCategoryId(categoryId);
    }

    @Override
    public Goods getGoodsById(Integer goodsId) {
        return indexMapper.getGoodsById(goodsId);
    }

    @Override
    public List<Comment> getCommentByGoodsById(Integer goodsId) {
        return indexMapper.getCommentByGoodsById(goodsId);
    }

    @Override
    public void addCart(Cart cart) {
        System.out.println("indexMapper.selectCart(cart):  "+indexMapper.selectCart(cart));
        if(indexMapper.selectCart(cart).size()==0)
        {
            System.out.println("333333");
            indexMapper.addCart(cart);

        }
        else {
            System.out.println("6666");
            indexMapper.updateCart(cart);
        }
    }

    @Override
    public Integer getCartCountById(Object id) {
        return indexMapper.getCartCountById(id);
    }

    @Override
    public List<Cart> getCartList(Object id) {
        return indexMapper.getCartList(id);
    }

    @Override
    public void updadeCartCount(Cart cart) {
        indexMapper.updadeCartCount(cart);
    }

    @Override
    public void deleteCartById(Integer id) {
        indexMapper.deleteCartById(id);
    }

    @Override
    public List<Address> getAddressList(Object id) {
        return indexMapper.getAddressList(id);
    }

    @Override
    public Cart checkOrder(Integer cartId) {
        return indexMapper.checkOrder(cartId);
    }

    @Override
    public User getUserById(Object id) {
        return indexMapper.getUserById(id);
    }

    @Override
    public List<Map<String, Object>> getOrderListByStatus(String status, Integer userId) {
        if (status.equals("all")){
            List<OrderId> orderIds = indexMapper.getOrderIds(userId);
            List<Map<String,Object>> goods = new ArrayList<>();
            for (int i=0;i<orderIds.size();i++){
                List<OrderItem> list = indexMapper.getOrderListAll(orderIds.get(i).getId());
                int totalPrice = 0;
                int totalCount = 0;
                for (int j = 0;j<list.size();j++){
                    totalPrice+=list.get(j).getGoodsPrice()*list.get(j).getGoodsNum();
                    totalCount+=list.get(j).getGoodsNum();
                }
                if(list.size()>0){
                    Map<String,Object> map =new HashMap<>();
                    map.put("totalPrice",totalPrice);
                    map.put("totalCount",totalCount);
                    map.put("createTime",orderIds.get(i).getCreateTime());
                    map.put("goods",list);
                    goods.add(map);
                }

            }
            return goods;
        }
        else {
            List<OrderId> orderIds = indexMapper.getOrderIds(userId);
            List<Map<String,Object>> goods = new ArrayList<>();
            for (int i=0;i<orderIds.size();i++){
                List<OrderItem> list = indexMapper.getOrderListByStatus(status,orderIds.get(i).getId());
                int totalPrice = 0;
                int totalCount = 0;
                for (int j = 0;j<list.size();j++){
                    totalPrice+=list.get(j).getGoodsPrice()*list.get(j).getGoodsNum();
                    totalCount+=list.get(j).getGoodsNum();
                }
                if(list.size()>0){
                    Map<String,Object> map =new HashMap<>();
                    map.put("totalPrice",totalPrice);
                    map.put("totalCount",totalCount);
                    map.put("createTime",orderIds.get(i).getCreateTime());
                    map.put("goods",list);
                    goods.add(map);
                }

            }
            return goods;
        }
    }

    @Override
    public void submitBuyNow(String remark, String goodsId, String goodsNum) {
        int id = Integer.parseInt(goodsId);
        int num = Integer.parseInt(goodsNum);
        System.out.println("remark: "+remark+"   goodsId:  "+id +"   goodsNum:  "+num);
        OrderItem order = new OrderItem();
    }

    @Override
    public void submitCart(String remark, String cartIds) {
        String[] cartids = cartIds.split(",");
        System.out.println("remark: "+remark+"   cartids:  "+Integer.parseInt(cartids[0]) );
        OrderItem order = new OrderItem();
//        for (int i=0;i< cartIds.length;i++){
//            deleteCartById(cartIds[i]);
//        }
    }
}
